from django.core.paginator import Paginator
from django.shortcuts import render, redirect
from .models import Article, Type


# Create your views here.
# def hello_world(request):
# return HttpResponse('hello world')


# 记录首页
def index_all(request, n, page):
    x = True
    if request.user.is_authenticated:
        x = True
    else:
        x = False
    type_list = Type.objects.all()
    if n == 0:
        article_list = Article.objects.all().order_by('publish_date')
    else:
        article_list = Article.objects.filter(type=n).order_by('publish_date')
    if page:
        page = int(page)
    else:
        page = 1
    page_ = Paginator(article_list, 10)
    page_num = page_.num_pages
    page_list = page_.page(page)
    if page_list.has_next():
        next_page = page+1
    else:
        next_page = 0
    if page_list.has_previous():
        previous_page = page-1
    else:
        previous_page = 0
    for xxx in range(1, page_num+1):
        print(xxx)
    return render(request, 'resource/index_all.html',
                      {
                          'type_list': type_list,
                          'n': n,
                          'x': x,
                          'article_list': page_list,
                          'curr_page': page,
                          'page_num': range(1, page_num + 1),
                          'next_page': next_page,
                          'previous_page': previous_page
                      }
                      )


# 搜索
def search(request):
    x = request.GET.get('info')
    type_list = Type.objects.all()
    if x != "":
        article_list = Article.objects.filter(title__icontains=x)
        article_list1 = Article.objects.filter(content__icontains=x)
        if len(article_list) == 0 and len(article_list1) == 0:
            return render(request, 'resource/index_all.html',
                          {
                              'article_list': ["未找到相关结果"],
                              'type_list': type_list,
                              'n': 999,
                              'info': x
                          }
                          )
        else:
            return render(request, 'resource/index_all.html',
                          {
                              'article_list': article_list | article_list1,
                              'type_list': type_list,
                              'n': 999,
                              'info': x
                          }
                          )
    else:
        return render(request, 'resource/index_all.html',
                      {
                          'article_list': ["请输入至少一个字符"],
                          'type_list': type_list,
                          'n': 999,
                          'info': x
                      }
                      )


# 文章详情页面
def article_page(requests, article_id, n):
    if n == 0:
        article_list = Article.objects.all()
    else:
        if n != 999:
            article_list = Article.objects.filter(type=n)
        else:
            article_list = Article.objects.filter(article_id=article_id)
    a = b = c = 0
    len1 = len(article_list)
    for i in range(0, len1):
        if i == 0:
            a = 0
            b = i
            if len1 == 1:
                c = i
            else:
                c = i + 1
        elif i == len1 - 1:
            a = i - 1
            b = i
            c = i
        else:
            a = i - 1
            b = i
            c = i + 1
        if article_list[b].article_id == article_id:
            break
    x_p_list = article_list[b].content.split('\n')
    return render(requests, 'resource/article.html',
                  {
                      'x': article_list[b],
                      'x_p_list': x_p_list,
                      'previous': article_list[a],
                      'next': article_list[c],
                      'n': n
                  }
                  )


# 发布问题
def upload_wz(requests):
    if not requests.user.is_authenticated:
        return redirect("/login")
    if requests.method == 'GET':
        type_list = Type.objects.all()  # select * from Type
        return render(requests, 'resource/wz.html',
                      {
                          'type_list': type_list
                      }
                      )
    if requests.method == 'POST' and requests.POST:
        title = requests.POST.get('title')
        if title == '':
            return render(requests, 'resource/wz.html',
                          {
                              'result': '标题不能为空',
                          }
                          )
        content = requests.POST.get('contents')
        if content == '':
            return render(requests, 'resource/wz.html',
                          {
                              'result': '正文不能为空',
                          }
                          )
        n = requests.POST.get('type')
        type_ = Type.objects.get(id=n)
        article = Article(title=title, content=content, author=requests.user, type=type_)
        article.save()
        return render(requests, 'resource/wz.html',
                      {
                          'result': '发布成功',
                      }
                      )


# 修改问题页
def reload_wz(request, n):
    if not request.user.is_authenticated:
        return redirect("/login")
    problem = Article.objects.get(article_id=n)
    type_list = Type.objects.all()
    return render(request, 'resource/reload.html',
                  {
                      'type_list': type_list,
                      'problem': problem
                  }
                  )


# 修改处理
def reload(request):
    title = request.POST.get('title')
    if title == '':
        return render(request, 'resource/reload.html',
                      {
                          'result': '标题不能为空请返回(未生效)',
                      }
                      )
    content = request.POST.get('contents')
    if content == '':
        return render(request, 'resource/reload.html',
                      {
                          'result': '正文不能为空请返回(未生效)',
                      }
                      )
    n = request.POST.get('type')
    article_id = request.POST.get('id')
    type_ = Type.objects.get(id=n)
    article = Article.objects.get(article_id=article_id)
    article.title = title
    article.content = content
    article.author = request.user
    article.type = type_
    article.save()
    return render(request, 'resource/reload.html',
                  {
                      'result': '更新成功',
                      'id': article_id,
                      'type': n
                  }
                  )


def index_redirect(requests):  # 重定向
    return redirect("/")
